<?php
/**
* @name Maiji
* @author pd87@qq.com
* @copyright m-j.cc
* @version v1.0
*/
namespace MAIJI;

class Upload
{
	protected $fileName;
	protected $maxSize = 5242880;
	protected $allowMime = ['image/jpeg','image/png','image/gif'];
	protected $allowExt = ['jpeg','jpg','png','gif'];
	protected $uploadPath;
	protected $imgFlag = true;
	protected $fileInfo;
	protected $error;
	protected $ext;
	
	/* 
		初始化
	*/
	public function __construct($fileName = 'pic',$uploadPath = '',$filter = true){
		if(empty($uploadPath)){
			$this -> uploadPath = APP_RUN_PATH.'data/images/'.date("m").'/';
		}else{
			$this -> uploadPath = APP_RUN_PATH.$uploadPath;
		}
		$this -> fileName = $fileName;
		if(isset($_FILES[$this -> fileName])){
			$this -> fileInfo = $_FILES[$this -> fileName];
		}
	}
	
	/**
	 * 检测上传文件是否出错
	 * @return boolean
	 */
	protected function checkError(){
		if(!is_null($this -> fileInfo)){
			if(is_array($this -> fileInfo['error'])){
				foreach($this -> fileInfo['error'] as $k => $a){
					if(!empty($a)){
						if(empty($this -> fileInfo['name'][$k]) && empty($this -> fileInfo['type'][$k])){
							return true;
						}
						switch($a){
							case 1:
								$this -> error='超过了PHP配置文件中upload_max_filesize选项的值';
								break;
							case 2:
								$this -> error='超过了表单中MAX_FILE_SIZE设置的值';
								break;
							case 3:
								$this -> error='文件部分被上传';
								break;
							case 4:
								$this -> error='没有选择上传文件';
								break;
							case 6:
								$this -> error='没有找到临时目录';
								break;
							case 7:
								$this -> error='文件不可写';
								break;
							case 8:
								$this -> error='由于PHP的扩展程序中断文件上传';
								break;
						}
						return false;
					}
				}
				return true;
			}else{
				if($this -> fileInfo['error']>0){
					switch($this -> fileInfo['error']){
						case 1:
							$this -> error='超过了PHP配置文件中upload_max_filesize选项的值';
							break;
						case 2:
							$this -> error='超过了表单中MAX_FILE_SIZE设置的值';
							break;
						case 3:
							$this -> error='文件部分被上传';
							break;
						case 4:
							$this -> error='没有选择上传文件';
							break;
						case 6:
							$this -> error='没有找到临时目录';
							break;
						case 7:
							$this -> error='文件不可写';
							break;
						case 8:
							$this -> error='由于PHP的扩展程序中断文件上传';
							break;
					}
					return false;
				}else{
					return true;
				}
			}
		}else{
			$this -> error='文件上传出错';
			return false;
		}
	}
	
	/**
	 * 检测上传文件的大小
	 * @return boolean
	 */
	protected function checkSize(){
		if(is_array($this -> fileInfo['size'])){
			foreach($this -> fileInfo['size'] as $a){
				if($a > $this -> maxSize){
					$this -> error = '上传文件过大';
					return false;
				}
			}
			return true;
		}else{
			if($this -> fileInfo['size']>$this -> maxSize){
				$this -> error='上传文件过大';
				return false;
			}
			return true;
		}
	}
	
	/**
	 * 检测扩展名
	 * @return boolean
	 */
	protected function checkExt(){
		if(is_array($this -> fileInfo['name'])){
			foreach($this -> fileInfo['name'] as $a){
				if(!empty($a)){
					$this -> ext[] = strtolower(pathinfo($a,PATHINFO_EXTENSION));
					if(!in_array($this -> ext[count($this -> ext) - 1],$this -> allowExt)){
						$this -> error = '不允许的扩展名';
						return false;
					}
				}
			}
			return true;
		}else{
			$this -> ext = strtolower(pathinfo($this -> fileInfo['name'],PATHINFO_EXTENSION));
			if(!in_array($this -> ext,$this -> allowExt)){
				$this -> error='不允许的扩展名';
				return false;
			}
			return true;
		}
	}
	
	/**
	 * 检测文件的类型
	 * @return boolean
	 */
	protected function checkMime(){
		if(is_array($this -> fileInfo['type'])){
			foreach($this -> fileInfo['type'] as $a){
				if(!empty($a)){
					if(!in_array($a,$this -> allowMime)){
						$this -> error='不允许的文件类型';
						return false;
					}
				}
			}
			return true;
		}else{
			if(!in_array($this -> fileInfo['type'],$this -> allowMime)){
				$this -> error='不允许的文件类型';
				return false;
			}
			return true;
		}
	}
	
	/**
	 * 检测是否是真实图片
	 * @return boolean
	 */
	protected function checkTrueImg(){
		if($this -> imgFlag){
			if(is_array($this -> fileInfo['tmp_name'])){
				foreach($this -> fileInfo['tmp_name'] as $a){
					if(!empty($a)){
						if(!@getimagesize($a)){
							$this -> error='不是真实图片';
							return false;
						}
					}
				}
				return true;
			}else{
				if(!@getimagesize($this -> fileInfo['tmp_name'])){
					$this -> error='不是真实图片';
					return false;
				}
				return true;
			}
		}
	}
	
	/**
	 * 检测是否通过HTTP POST方式上传上来的
	 * @return boolean
	 */
	protected function checkHTTPPost(){
		if(is_array($this -> fileInfo['tmp_name'])){
			foreach($this -> fileInfo['tmp_name'] as $a){
				if(!empty($a)){
					if(!is_uploaded_file($a)){
						$this -> error='文件不是通过HTTP POST方式上传上来的';
						return false;
					}
				}
			}
			return true;
		}else{
			if(!is_uploaded_file($this -> fileInfo['tmp_name'])){
				$this -> error='文件不是通过HTTP POST方式上传上来的';
				return false;
			}
			return true;
		}
	}
	
	/**
	 *显示错误 
	 */
	protected function showError(){
		exit('<span style="color:red">'.$this -> error.'</span>');
	}
	
	/**
	 * 检测目录不存在则创建
	 */
	protected function checkUploadPath(){
		if(!file_exists($this -> uploadPath)){
			createFolders($this -> uploadPath);
		}
	}
	
	/**
	 * 产生唯一字符串
	 * @return string
	 */
	protected function getUniName(){
		// return md5(uniqid(microtime(true),true));
		return time().rand(100,999);
	}
	
	/**
	 * 上传文件
	 * @return string
	 */
	public function upfile(){
		if($this -> checkError()&&$this -> checkSize()&&$this -> checkExt()&&$this -> checkMime()&&$this -> checkTrueImg()&&$this -> checkHTTPPost()){
			$this -> checkUploadPath();
			if(is_array($this -> fileInfo['tmp_name'])){
				foreach($this -> fileInfo['tmp_name'] as $k => $v){
					if(!empty($v)){
						$this -> uniName = $this -> getUniName();
						$this -> destination[$k] = $this -> uploadPath.$this -> uniName.'.'.$this -> ext[$k];
						if($filter){
							(new \MAIJI\imgcompress($this -> destination[$k],1))->compressImg($this -> destination[$k]);
						}
						$this -> destination[$k] = str_replace('\\','/',$this -> destination[$k]);
						$this -> destination[$k] = str_replace($_SERVER['DOCUMENT_ROOT'],'',$this -> destination[$k]);
						if(!move_uploaded_file($this -> fileInfo['tmp_name'][$k],$this -> destination[$k])){
							$this -> error='文件移动失败';
							$this -> showError();
						}
					}
				}
				return  $this -> destination;
			}else{
				$this -> uniName = $this -> getUniName();
				$this -> destination = $this -> uploadPath.$this -> uniName.'.'.$this -> ext;
				if(@move_uploaded_file($this -> fileInfo['tmp_name'],$this -> destination)){
					if($filter){
						(new \MAIJI\imgcompress($this -> destination,1))->compressImg($this -> destination);
					}
					$this -> destination = str_replace('\\','/',$this -> destination);
					$this -> destination = str_replace($_SERVER['DOCUMENT_ROOT'],'',$this -> destination);
					return $this -> destination;
				}else{
					$this -> error='文件移动失败';
					$this -> showError();
				}
			}
		}else{
			$this -> showError();
		}
	}
}